﻿using MES.Desktop.Common;
using MES.Desktop.IService;
using MES.Desktop.SystemModule.Models;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MES.Desktop.SystemModule.ViewModels.Dialogs
{
    public class ModifyMenuViewModel : DialogViewModelBase
    {
        public MenuModel MenuModel { get; set; } =
                new MenuModel();

        public List<Entities.MenuEntity> ParentNodes { get; set; } =
                new List<Entities.MenuEntity>();

        public ObservableCollection<FuncModel> FuncList { get; set; } =
            new ObservableCollection<FuncModel>();

        private string _funcIds = "";
        private string _funcNames;

        public string FuncNames
        {
            get { return _funcNames; }
            set { SetProperty<string>(ref _funcNames, value); }
        }


        IMenuService _menuService;
        public ModifyMenuViewModel(IMenuService menuService, IFuncService funcService)
        {
            _menuService = menuService;

            Task.Run(() =>
            {
                var funcs = funcService.GetAllFunctions();
                foreach (var item in funcs)
                {
                    var model = new FuncModel
                    {
                        IsSelected = _funcIds.Contains(item.FuncId.ToString()),
                        FuncId = item.FuncId,
                        FuncName = item.FuncName
                    };
                    model.PropertyChanged += (se, ev) =>
                    {
                        FuncNames = string.Join(",", FuncList.Where(f => f.IsSelected).Select(f => f.FuncName));
                    };
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        FuncList.Add(model);
                    });
                   
                }
                // 第一次打开编辑窗口时，显示默认功能名称
                FuncNames = string.Join(",", FuncList.Where(f => f.IsSelected).Select(f => f.FuncName));
            });
           
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            var model = parameters.GetValue<MenuModel>("model");
            ParentNodes = parameters.GetValue<List<Entities.MenuEntity>>("parents");
            ParentNodes.Insert(0, new Entities.MenuEntity { MenuHeader = "ROOT", MenuId = "-1" });

            if (model == null)
            {
                Title = "新增菜单项";
                MenuModel.ParentId = "-1";
                MenuModel.MenuId = "";
            }
            else
            {
                Title = "编辑菜单项";

                MenuModel.MenuId = model.MenuId;
                MenuModel.MenuHeader = model.MenuHeader;
                MenuModel.ParentId = model.ParentId;
                MenuModel.TargetView = model.TargetView;
                MenuModel.MenuIcon = model.MenuIcon;

                _funcIds = model.FuncIds;
            }
        }

        public override void DoSave()
        {
            try
            {
                _funcIds = string.Join(",", FuncList.Where(f => f.IsSelected).Select(f => f.FuncId));
                Entities.MenuEntity menuEntity = new Entities.MenuEntity
                {
                    MenuId = MenuModel.MenuId,
                    MenuHeader = MenuModel.MenuHeader,
                    // 需要把字体字符转成编码  \ue618
                    MenuIcon = ((int)MenuModel.MenuIcon.ToArray()[0]).ToString("x"),
                    TargetView = MenuModel.TargetView,
                    ParentId = MenuModel.ParentId,
                    Funcs = _funcIds,
                    State = 1
                };

                _menuService.UpdateMenu(menuEntity);

                base.DoSave();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
